var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#app',
    data:{
        q:{
            username: null
        },
        showList: false,
        title:null,
        roleList:{},
        user:{
            username: '',
            email: '',
            moblie: '',
            status:1,
            deptId:null,
            deptName:null,
            roleIdList:[2]
        },
        authStatus:false,
        scoreList: []
    },
    mounted:function() {
        this.getLoginStatus();
        this.getUser();
        this.getSenicScoreInfo();
    },
    methods: {
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getUser(userId);
            //获取角色信息
            this.getRoleList();
        },
        getLoginStatus:function(){
            $.ajax({
                type: "get",
                url: baseURL + "/sys/user/info/",
                cache: false,
                success: (res) => {

                }
            });
        },
        getTime:function (value){
            var date=new Date(parseInt(value));
            let localTime = date.toLocaleDateString()
            return localTime
        },
        getSenicScoreInfo:function(index){
            baseURL = "../../"
            $.get(baseURL + "senic/senicscores/myscorelist", function(r){
    
                vm.scoreList = r.senicScoresInfo;
                console.log(r)
            });
        },
        saveOrUpdate: function () {
            var bvObj = $("#appForm").data("bootstrapValidator");
            bvObj.validate()
            const res = bvObj.isValid();
            if (res) {
                vm.user.roleIdList = [2];
                var url = vm.user.userId == null ? "sys/user/save" : "sys/user/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.user),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功');
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }
        },
        getUser: function(userId){
            $.ajax({
                type: "get",
                url: baseURL + "sys/user/myinfo",
                cache: false,
                success: (res) => {
                    vm.user = res.user;
                    vm.user.password = null;
                }
            });
        },
        getRoleList: function(){
            $.get(baseURL + "sys/role/select", function(r){
                vm.roleList = r.list;
            });
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.user.deptId = node[0].deptId;
                    vm.user.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
        }
    }
});

$('#appForm').bootstrapValidator({
    // 默认的提示消息
    message: 'This value is not valid',
    live: 'enabled',
    // 表单框里右侧的icon
    feedbackIcons: {
    valid: 'glyphicon glyphicon-ok',
    invalid: 'glyphicon glyphicon-remove',
    validating: 'glyphicon glyphicon-refresh'
    },
    submitHandler: function (validator, form, submitButton) {
      // 表单提交成功时会调用此方法
      // validator: 表单验证实例对象
      // form  jq对象  指定表单对象
      // submitButton  jq对象  指定提交按钮的对象
    },
    fields: {
      email: {
        validators: {
          notEmpty: {
            message: '邮箱地址不能为空'
          },
          emailAddress: {
            regexp: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '邮箱地址格式有误'
          }
        }
      },
      phone: {
        validators: {
          notEmpty: {
            message: '电话不能为空'
          },
          regexp: {
            regexp: /^[1][3,4,5,7,8,9][0-9]{9}$/,
            message: '请输入正确的手机号'
          }
        }
      },
      password: {
        validators: {
          regexp: {
            regexp: /^[A-Za-z0-9]+$/,
            message: '只能用英文或数字作为密码'
          }
        }
      }
    }
});